package pers.liuchengyin.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import pers.liuchengyin.config.EsConfig;
import pers.liuchengyin.constants.EsConstants;
import pers.liuchengyin.constants.MappingProperty;
import pers.liuchengyin.pojo.Result;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @ClassName EsIndexSerivce
 * @Description
 * @Author 柳成荫
 * @Date 2022/4/1
 */
@Service
public class EsIndexService {
    @Qualifier("elasticSearchRestClient")
    @Autowired
    private RestHighLevelClient esClient;

    public Boolean indexIsExist(String indexName) {
        try {
            return esClient.indices().exists(new GetIndexRequest(indexName), EsConfig.COMMON_OPTIONS);
        } catch (IOException e) {
            e.printStackTrace();
            return Boolean.FALSE;
        }
    }

    public Boolean createIndex(String indexName){
        return createIndex(indexName, null);
    }

    public Boolean createIndex(String indexName, Map<String, ?> properties){
        return createIndex(indexName, null, null, properties);
    }

    public Boolean createIndex(String indexName, Map<String, ?> aliases, Map<String, ?> settings, Map<String, ?> properties) {
        if (StringUtils.isBlank(indexName) || indexIsExist(indexName)) {
            return Boolean.FALSE;
        }
        CreateIndexRequest request = new CreateIndexRequest(indexName);
        if (!CollectionUtils.isEmpty(aliases)) {
            request.aliases(aliases);
        }
        if (!CollectionUtils.isEmpty(settings)) {
            request.settings(settings);
        }
        if (!CollectionUtils.isEmpty(properties)) {
            Map<String, Object> mapping = new HashMap<>();
            mapping.put(EsConstants.INDEX_MAPPING_PROPERTIES, properties);
            // 禁止自动添加字段
            mapping.put(EsConstants.INDEX_MAPPING_DYNAMIC, EsConstants.INDEX_MAPPING_DYNAMIC_STRICT);
            request.mapping(mapping);
        }
        try {
            CreateIndexResponse createIndexResponse = esClient.indices().create(request, EsConfig.COMMON_OPTIONS);
            return createIndexResponse.isAcknowledged();
        } catch (IOException e) {
            e.printStackTrace();
            return Boolean.FALSE;
        }
    }

    public Boolean deleteIndex(String indexName) {
        if (StringUtils.isBlank(indexName) || !indexIsExist(indexName)) {
            return Boolean.FALSE;
        }
        DeleteIndexRequest request = new DeleteIndexRequest(indexName);
        try {
            AcknowledgedResponse response = esClient.indices().delete(request, EsConfig.COMMON_OPTIONS);
            return response.isAcknowledged();
        } catch (IOException e) {
            e.printStackTrace();
            return Boolean.FALSE;
        }
    }


}
